package com.kjsdfgk.hjmdsf.history.mvp.ui.activity;

import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.SPUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import com.kjsdfgk.hjmdsf.app.entity.HistoryItem;
import com.kjsdfgk.hjmdsf.app.entity.ListBean;
import com.kjsdfgk.hjmdsf.app.entity.WebPhoto;
import com.kjsdfgk.hjmdsf.app.entity.XJVideoJson;
import com.kjsdfgk.hjmdsf.ftmztlist.mvp.ui.fragment.WebPhotoAdapter;
import com.kjsdfgk.hjmdsf.history.di.component.DaggerHistoryComponent;
import com.kjsdfgk.hjmdsf.history.mvp.contract.HistoryContract;
import com.kjsdfgk.hjmdsf.history.mvp.presenter.HistoryPresenter;
import com.kjsdfgk.hjmdsf.myphoto.mvp.ui.activity.MyPhotoActivity;
import com.kjsdfgk.hjmdsf.play.mvp.ui.activity.MyPlayVideoActivity;
import com.lksdkjs.live.R;
import com.myapp.video.home.mvp.ui.activity.PlayVideoActivity;


import org.litepal.LitePal;

import java.util.List;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 05/12/2020 12:26
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class HistoryActivity extends BaseActivity<HistoryPresenter> implements HistoryContract.View {

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerHistoryComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_history; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    HistoryAdapter adapter;
    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        GridLayoutManager layoutManager = new GridLayoutManager(this,3);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter = new HistoryAdapter(R.layout.item_history));


        String userName = SPUtils.getInstance().getString("account", "");
        List<HistoryItem> list = LitePal.where("userName=?",userName).order("time desc").limit(1000).find(HistoryItem.class);

        adapter.setNewData(list);

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                HistoryItem item = (HistoryItem) adapter.getData().get(position);
                if (item.type == 1000){
                    Intent intent = new Intent(HistoryActivity.this  , PlayVideoActivity.class);
                    intent.putExtra("url" ,item.url);
                    intent.putExtra("title" ,item.title);
                    intent.putExtra("img" ,item.img);

                    startActivity(intent);
                }else if (item.type == 2000){
                    Intent intent = new Intent(HistoryActivity.this , MyPlayVideoActivity.class);
                    ListBean xj = new ListBean();
                    xj.setVod_pic(item.img);
                    xj.setVod_id(Integer.parseInt(item.url));
                    xj.setVod_name(item.title);
                    xj.setType_id(2000);
                    intent.putExtra("bean" , xj );

                    startActivity(intent);
                }else if (item.type == 2100){
                    Intent intent = new Intent(HistoryActivity.this , MyPlayVideoActivity.class);
                    ListBean xj = new ListBean();
                    //  HistoryItem historyItem = new HistoryItem();
                    //            historyItem.img = xjVideo.getVod_pic();
                    //            historyItem.url = xjVideo.getVod_tag();
                    //            historyItem.title = xjVideo.getVod_name();
                    //            historyItem.time = TimeUtils.getNowString();
                    //            historyItem.type = 2100;
                    xj.setVod_pic(item.img);
                    xj.setVod_tag(item.url);
                    xj.setVod_name(item.title);
                    xj.setType_id(2100);
                    intent.putExtra("bean" , xj );

                    startActivity(intent);
                }else if (item.type == 2200){
                    Intent intent = new Intent(HistoryActivity.this , MyPlayVideoActivity.class);
                    ListBean xj = new ListBean();
                    //  HistoryItem historyItem = new HistoryItem();
                    //            historyItem.img = xjVideo.getVod_pic();
                    //            historyItem.url = xjVideo.getVod_tag();
                    //            historyItem.title = xjVideo.getVod_name();
                    //            historyItem.time = TimeUtils.getNowString();
                    //            historyItem.type = 2100;
                    xj.setVod_pic(item.img);
                    xj.setVod_tag(item.url);
                    xj.setVod_name(item.title);
                    xj.setType_id(2200);
                    intent.putExtra("bean" , xj );

                    startActivity(intent);
                }else if (item.type == 3000){
                    Intent intent = new Intent(HistoryActivity.this , MyPhotoActivity.class);
                    WebPhoto webPhoto = new WebPhoto();
                    webPhoto.url = item.img;
                    webPhoto.href = item.url;
                    webPhoto.alt = item.title;
                    webPhoto.imgNum = item.imgNum;
                    intent.putExtra("webPhoto",webPhoto);

                    startActivity(intent );
                }
            }
        });

    }


    @Override
    public void showLoading(String key) {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public boolean isCancel() {
        return false;
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }


    @Override
    protected void onResume() {
        super.onResume();
        if (adapter!=null){
            String userName = SPUtils.getInstance().getString("account", "");
            List<HistoryItem> list = LitePal.where("userName=?",userName).order("time desc").find(HistoryItem.class);
            adapter.setNewData(list);
        }

    }
}
